#include "stats_monitor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 统计监视器结构：负责收集和管理存储系统的统计信息
struct stats_monitor {
    replacement_log_entry_t* replacement_logs; // 替换日志条目数组
    uint32_t replacement_count;               // 当前替换日志数量
    uint32_t replacement_capacity;            // 替换日志最大容量
};

// 创建统计监视器
// 返回值：
//   成功返回统计监视器指针，失败返回NULL
stats_monitor_t* stats_monitor_create() {
    stats_monitor_t* monitor = malloc(sizeof(stats_monitor_t));
    if (!monitor) {
        return NULL;
    }

    monitor->replacement_capacity = 100;
    monitor->replacement_count = 0;
    monitor->replacement_logs = malloc(monitor->replacement_capacity * sizeof(replacement_log_entry_t));

    if (!monitor->replacement_logs) {
        free(monitor);
        return NULL;
    }

    return monitor;
}

// 记录替换事件
// 参数：
//   monitor - 统计监视器指针
//   evicted_page - 被替换的页面ID
//   new_page - 新页面ID
//   was_dirty - 被替换页面是否为脏页
//   policy - 使用的替换策略
void stats_monitor_log_replacement(stats_monitor_t* monitor, 
    page_id_t evicted_page, page_id_t new_page, 
    int was_dirty, cache_replacement_policy_t policy) {
    if (!monitor) return;

    if (monitor->replacement_count >= monitor->replacement_capacity) {
        uint32_t new_capacity = monitor->replacement_capacity * 2;
        replacement_log_entry_t* new_logs = realloc(monitor->replacement_logs,
            new_capacity * sizeof(replacement_log_entry_t));
        if (!new_logs) {
            return;
        }
        monitor->replacement_logs = new_logs;
        monitor->replacement_capacity = new_capacity;
    }

    replacement_log_entry_t* entry = &monitor->replacement_logs[monitor->replacement_count++];
    entry->evicted_page = evicted_page;
    entry->new_page = new_page;
    entry->timestamp = (uint32_t)time(NULL);
    entry->was_dirty = was_dirty;
    entry->policy = policy;
}

// 记录替换事件
void stats_monitor_destroy(stats_monitor_t* monitor) {
    if (monitor) {
        free(monitor->replacement_logs);
        free(monitor);
    }
}

// 记录替换事件
uint32_t stats_monitor_get_replacement_count(stats_monitor_t* monitor) {
    return monitor ? monitor->replacement_count : 0;
}

// 打印统计信息
// 修改stats_monitor_print_cache_stats函数，修复中文字符显示
void stats_monitor_print_cache_stats(uint32_t hits, uint32_t misses, uint32_t cache_size, uint32_t cache_capacity) {
    double hit_ratio = 0.0;
    if (hits + misses > 0) {
        hit_ratio = (double)hits / (hits + misses) * 100;
    }
    
    // 使用英文显示统计信息，避免编码问题
    printf("Cache Statistics:\n");
    printf("Cache hits: %u\n", hits);
    printf("Cache misses: %u\n", misses);
    printf("Hit ratio: %.2f%%\n", hit_ratio);
    printf("Current cache size: %u/%u\n", cache_size, cache_capacity);
}

// 打印统计信息
void stats_monitor_print_replacement_log(stats_monitor_t* monitor) {
    if (!monitor) return;

    printf("=== 页面替换日志 ===\n");
    printf("替换次数: %u\n\n", monitor->replacement_count);

    if (monitor->replacement_count == 0) {
        printf("无替换记录\n");
        return;
    }

    for (uint32_t i = 0; i < monitor->replacement_count; i++) {
        replacement_log_entry_t* entry = &monitor->replacement_logs[i];
        printf("替换记录 #%u:\n", i + 1);
        printf("  被替换页: %u\n", entry->evicted_page);
        printf("  新调入页: %u\n", entry->new_page);
        printf("  时间戳: %u\n", entry->timestamp);
        printf("  脏页: %s\n", entry->was_dirty ? "是" : "否");
        printf("  替换策略: %s\n", entry->policy == CACHE_LRU ? "LRU" : "FIFO");
        printf("\n");
    }
}